package cn.tedu.util;

import java.sql.*;

public class JDBCUtil {
    /**
     * 注册驱动
     */
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    public static Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/gdb2109?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url,"root","root");
        return conn;
    }

    /**
     * 关闭资源
     * @param conn
     * @param stat
     * @param rs
     */
    public static void close(Connection conn, Statement stat, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        Connection con = JDBCUtil.getConnection();
        System.out.println(con);
    }
}








